local parseConfig = {}

--解析出json的字段内容
local function parse_json_data(jsonData)
    if not jsonData then 
        log.error("parse_json_data jsonData = nil !")
        return jsonData
    end

    local ok, data = pcall(cjson.decode, jsonData)
    if ok then
        return data    
    else
        log.error("parse_json_data cjson.decode error jsonData=", jsonData)
    end

    return jsonData    
end

--解析游戏配置
function parseConfig.parse_game_config(config)
    local game_config = {}

    for _, oneGameInfo in pairs(config or {}) do
        --[key]game_label  jscon数据解出来
        oneGameInfo.game_label = parse_json_data(oneGameInfo.game_label)
        --
        local info = {
            --是否开启
            is_open = true,
            --游戏名字 
            game_name = oneGameInfo.gamename,
            --排序序号
            sort_index = oneGameInfo.sort,         
            --税率            
            system_tax = oneGameInfo.systemtax,
            reward_tax = oneGameInfo.rewardtax,
            dark_tax = oneGameInfo.darktax,
            --玩家范围
            min_player = oneGameInfo.min_player,
            max_player = oneGameInfo.max_player,
            --游戏标签
            game_tab = oneGameInfo.game_label or {0},     
            --1开启2维护中3关闭      
            status = oneGameInfo.status,       
            --平台id, 1为自研 2为ky                 
            attribution = oneGameInfo.attribution,              
            robot_switch = (oneGameInfo.robot_switch == 1),
            --是否支持好友约战
            friend_switch = (oneGameInfo.friend_switch == 1),  
            --是否上庄  
            b_up_banker = oneGameInfo.b_up_banker, 
            --是否需要设置携带 
            needChooseCarry = oneGameInfo.needChooseCarry,                
        }
        game_config[oneGameInfo.game_id] = info
    end

    return game_config
end

--解析游戏配置: 如果字段名和数据库表不一致,可能存储过程中as了
function parseConfig.parse_gameroom_config(config, GameConfig)  
    local room_config = {} 

    for _, oneGameRoomInfo in pairs(config) do
        --json格式的数据先解出来
        oneGameRoomInfo.minimumchip = parse_json_data(oneGameRoomInfo.minimumchip)
        oneGameRoomInfo.robotcarry = parse_json_data(oneGameRoomInfo.robotcarry)
        --log.info("oneGameRoomInfo.minimumchip=", oneGameRoomInfo.minimumchip)
        --log.info("oneGameRoomInfo.minimumchip[1]=", oneGameRoomInfo.minimumchip[1])
        local info = {
            room_level = oneGameRoomInfo.room_level,
            --底注,获取游戏房间列表协议时需要字符串
            room_base_note = KeepDecimal(tonumber(oneGameRoomInfo.minimumchip[1])),   
            --最大下注 只有百人场存在     
            room_max_note = KeepDecimal(tonumber(oneGameRoomInfo.minimumchip[2])), 
            --准入 [DB-key]entrance    
            room_min_entrance = tonumber(oneGameRoomInfo.getchip) or 0,   
            --房间名字      
            room_info = oneGameRoomInfo.roomname,   
            --机器人带入范围                
            robot_carry = oneGameRoomInfo.robotcarry,
            --抢红包特有?
            rob_banker = oneGameRoomInfo.robbanker,
            --下注筹码
            bet_beishu = oneGameRoomInfo.bets,
            --限红
            bet_limit = oneGameRoomInfo.limitred,
            --房间号
            room_id  = oneGameRoomInfo.roomid,
            --有的游戏有奖池库存id
            prize_stock_id = oneGameRoomInfo.prizestockid,    
            --上庄的限制     
            up_banker_config = oneGameRoomInfo.up_banker_config,    
        }

        --x游戏下的某一等级房间(场次)是否打开
        if oneGameRoomInfo.switch then     
            --level有可能不是连续的 
            --log.info("room_config1111=",room_config)
            --log.info("oneGameRoomInfo.game_id=", oneGameRoomInfo.game_id)
            --log.info("oneGameRoomInfo.room_level=", oneGameRoomInfo.room_level)
            room_config[oneGameRoomInfo.game_id] = room_config[oneGameRoomInfo.game_id] or {}
            room_config[oneGameRoomInfo.game_id][oneGameRoomInfo.room_level] = info   
            --log.info("room_config2222=",room_config)  
        end
    end

    return room_config
end

--[[
--解析游戏配置: 如果字段名和数据库表不一致,可能存储过程中as了
function parseConfig.parse_gameroom_config2(config, GameConfig)         
    local room_config = {}
    for k, v in pairs(config or {}) do
        local gameid = math.floor(k / 1000)
        room_config[gameid] = room_config[gameid] or {}
        local gameconfig = GameConfig[gameid] or {}
        local room_level = tonumber(v.roomlevel) or (k % 1000)
        local min_entrance = tonumber(v.getchip) or 0
        if min_entrance % 1 == 0 then
            min_entrance = math.floor(min_entrance)    --100.0 => 100
        end
        local info = {
            room_level = room_level,
            room_base_note = v.minimumchip and v.minimumchip[1],        --底注,获取游戏房间列表协议时需要字符串
            room_max_note = v.minimumchip and v.minimumchip[2],         --最大下注 只有百人场存在
            room_min_entrance = min_entrance,         --准入
            room_info = v.roomname,                   --房间名字
            robot_carry = v.robotcarry,
            rob_banker = v.robbanker,
            bet_beishu = v.bets,
            bet_limit  = v.limitred,
            room_id  = v.roomid,
            prize_stock_id = v.prizestockid,          --有的游戏有奖池库存id
            up_banker_config = v.up_banker_config,    --上庄的限制

            --下面都是游戏的配置，复制到房间
            system_tax = gameconfig.system_tax,
            reward_tax = gameconfig.reward_tax,
            dark_tax = gameconfig.dark_tax,
            min_player = gameconfig.min_player,
            max_player = gameconfig.max_player,
            robot_switch = gameconfig.robot_switch,
            needChooseCarry = gameconfig.needChooseCarry,      --是否需要设置携带
            b_up_banker = gameconfig.b_up_banker,              --是否上庄
        }
        --狼人杀3人桌和5人桌
        if gameid == g_gameType.LRS then           
            if room_level <= 3 then
                info.min_player, info.max_player = 3, 3
            else
                info.min_player, info.max_player = 5, 5
            end
        end
        if v.switch then     --房间是否打开
            if room_level < 900 then    --抢红包房间特殊
                room_config[gameid][room_level] = info      --level有可能不是连续的
            end
        end
    end
    return room_config
end
--]]

--解析成就配置
function parseConfig.parse_achieve_config(achieve_config)
    local cardhit = {}
    local playgame = {}
    local wingame = {}
    local losegame = {}
    local winscore = {}
    local losescore = {}
    local onerecharge = {}
    local totalrecharge = {}
    local reachrank = {}
    local res = {by_id = {}, by_type = {}}

    for _, v in pairs(achieve_config) do
        if v.switch == 1 then
            v.related_game_id = parse_json_data(v.related_game_id)
            v.target = parse_json_data(v.target)

            res.by_id[v.achieve_id] = {
                achieve_id = v.achieve_id,
                todo_number = v.todo_number,
                achieve_des = v.achieve_des,
                url_img = v.url_img,
                sort_index = v.sort_index
            }

            --
            if v.achieve_type == g_achieveType.GotCardType then
                --获取牌型
                for _, card_type in pairs(v.target or {}) do
                    cardhit[card_type] = {aid = v.achieve_id}
                end
            elseif v.achieve_type == g_achieveType.TotalGameTimes then   
                --累计玩     
                table.insert(playgame, {aid = v.achieve_id})
            elseif v.achieve_type == g_achieveType.WinGameTimes then  
                --累计赢        
                table.insert(wingame, {aid = v.achieve_id})
            elseif v.achieve_type == g_achieveType.LoseGameTimes then
                --累计输         
                table.insert(losegame, {aid = v.achieve_id})
            elseif v.achieve_type == g_achieveType.OneWinScore then 
                --当局赢的钱          
                table.insert(winscore, {aid = v.achieve_id, num = v.todo_number})
            elseif v.achieve_type == g_achieveType.OneLoseScore then 
                --当局输的钱         
                table.insert(losescore,  {aid = v.achieve_id, num = v.todo_number})
            elseif v.achieve_type == g_achieveType.OneRecharge then
                --一笔最高充值           
                table.insert(onerecharge, {aid = v.achieve_id, num = v.todo_number})
            elseif v.achieve_type == g_achieveType.TotalRecharge then 
                --总充值        
                table.insert(totalrecharge, {aid = v.achieve_id, num = v.todo_number})
            elseif v.achieve_type == g_achieveType.ReachRank then
                --登录过排行榜             
                local top, down = 0, 0
                if v.target then
                    top  = v.target[1] or 0
                    down = v.target[2] or 0
                end
                table.insert(reachrank, {aid = v.achieve_id, num = v.todo_number, top = top, down = down})
            end
        end
    end

    res.by_type[g_achieveType.GotCardType] = cardhit
    res.by_type[g_achieveType.TotalGameTimes] = playgame
    res.by_type[g_achieveType.WinGameTimes] = wingame
    res.by_type[g_achieveType.LoseGameTimes] = losegame
    res.by_type[g_achieveType.OneWinScore] = winscore
    res.by_type[g_achieveType.OneLoseScore] = losescore
    res.by_type[g_achieveType.OneRecharge] = onerecharge
    res.by_type[g_achieveType.TotalRecharge] = totalrecharge
    res.by_type[g_achieveType.ReachRank] = reachrank

    return res
end

--解析vip配置
function parseConfig.parse_vip_config(vip_config)
    table.sort(vip_config, function(a, b) 
        return (a.level_id or 0) < (b.level_id or 0) 
    end)

    return vip_config
end

--解析牌型配置
function parseConfig.parse_card_config(card_config)                
    local cardconfig = {}
    for _, v in pairs(card_config or {}) do
        if v.value then
            cardconfig[v.game_id] = cardconfig[v.game_id] or {}
            cardconfig[v.game_id][v.value] = {card_id = v.card_id, multiple = v.multiple}
        end
    end

    return cardconfig
end


return parseConfig